//
//  FootViewController.m
//  PregnancyAPP
//
//  Created by Yangfan on 14-9-16.
//  Copyright (c) 2014年 APPYING. All rights reserved.
//

#import "FootViewController.h"
#import "FilesOperation.h"

@interface FootViewController ()

@end

@implementation FootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    self.navigationController.navigationBar.alpha = 1;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self showTheTopScroll];
    
    self.navigationController.navigationBar.barTintColor = HexRGB(0xffcbd8);
    
    if ((int)_pregnancyDay / 7 >= 40) {
        whichWeek.contentOffset = CGPointMake(64 * 39, 0);
    } else {
        whichWeek.contentOffset = CGPointMake(64 * (((int)_pregnancyDay - 1) / 7), 0);
    }
    
    
    [self showContentScroll:((int)_pregnancyDay - 1)/7];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *test = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, 20)];
    test.backgroundColor = HexRGB(0xfed2dd);
    [self.view addSubview:test];
 
    
    UIButton *backButton = [[UIButton alloc] init];
    backButton.frame = CGRectMake(0, 0, 44, 44);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton addTarget:self action:@selector(popToPreviousView) forControlEvents:UIControlEventTouchUpInside];
    [backButton setBackgroundImage:[UIImage imageNamed:@"backFoodPage"] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[UIImage imageNamed:@"backFoodPage"] forState:UIControlStateHighlighted];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem =leftItem;
    
    // 右侧的空白填充
    UIView *blaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    blaceView.backgroundColor = [UIColor clearColor];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:blaceView];
    self.navigationItem.rightBarButtonItem = right;
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Kwidth - 88 - 44, 44)];
    titleView.backgroundColor = [UIColor clearColor];
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 18, Kwidth - 88 - 44, 19)];
    titleLab.text = @"饮食推荐";
    titleLab.backgroundColor = [UIColor clearColor];
    titleLab.font = [UIFont boldSystemFontOfSize:18];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = HexRGB(0xed4f78);
    [titleView addSubview:titleLab];
    
    self.navigationItem.titleView = titleView;
    self.navigationItem.titleView.frame = CGRectMake(0, 0, Kwidth, 44);
    
    // 顶部的第几周的滚动选择
    whichWeek = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 74, Kwidth, 36)];
    whichWeek.delegate = self;
    whichWeek.showsHorizontalScrollIndicator = NO;
    whichWeek.backgroundColor = HexRGB(0xf55b82);
    [self.view addSubview:whichWeek];
    
    // 向下的白色指示标
    UIImageView *zhishijiantou = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zhishijiantou"]];
    zhishijiantou.frame = CGRectMake((Kwidth - 16)/2, 74, 16, 7);
    [self.view addSubview:zhishijiantou];
    
    // 每周内容的滚动显示
    contentScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 81 + 36, Kwidth, Kheight - 81 - 36 - 12)];
    [self.view addSubview:contentScroll];
    
    contentDic = [NSDictionary dictionaryWithContentsOfFile:[FilesOperation returnResourceFileAddress:@"news" andTyeo:@"plist"]];
}

// 顶部第几周滚动的显示
- (void)showTheTopScroll {
    allWeekImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"allWeek"]];
    allWeekImage.userInteractionEnabled = YES;
    allWeekImage.frame = CGRectMake(0, 0, 2816, 36);
    
//    for (int i = 0; i < 40; i++) {
//        if (i < 10) {
//            whichWeekButton = [[UIButton alloc] initWithFrame:CGRectMake(63.5 * i, 0, 63, 36)];
//        } else {
//            whichWeekButton = [[UIButton alloc] initWithFrame:CGRectMake(635 + 64 * (i - 10), 0, 63.5, 36)];
//        }
//        whichWeekButton.tag = i + 9999;
//        [whichWeekButton addTarget:self action:@selector(selectOneWeek:) forControlEvents:UIControlEventTouchUpInside];
//        whichWeekButton.backgroundColor = [UIColor clearColor];
////        [allWeekImage addSubview:whichWeekButton];
//    }
    
    [whichWeek addSubview:allWeekImage];
    whichWeek.contentSize = CGSizeMake(2816, 36);
}

// 第几周内容的显示
- (void)showContentScroll:(int) whichWeekInt {
    if (whichWeekInt > 39) {
        whichWeekInt = 39;
    } else if(whichWeekInt == 0) {
        whichWeekInt = 0;
    } else {
//        whichWeekInt = wh ichWeekInt - 1;
    }
    UIFont *font = [UIFont systemFontOfSize:13];
    CGSize size;
    NSString *str = nil;
    
    // 显示第几周
    if (huaiyunWhichWeek == nil) {
        huaiyunWhichWeek = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 100, 18)];
        huaiyunWhichWeek.text = [NSString stringWithFormat:@"怀孕第 %d 周", whichWeekInt + 1];
        huaiyunWhichWeek.textColor = HexRGB(0xed4f78);
        huaiyunWhichWeek.font = [UIFont boldSystemFontOfSize:17];
        [contentScroll addSubview:huaiyunWhichWeek];
    } else {
        huaiyunWhichWeek.text = [NSString stringWithFormat:@"怀孕第 %d 周", whichWeekInt + 1];
    }
    
    // 显示粗略信息
    str = [[contentDic objectForKey:[NSString stringWithFormat:@"%d", whichWeekInt + 1]] firstObject];
    str = [str stringByReplacingOccurrencesOfString:@"\\r" withString:@"\r"];
    str = [str stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    size = [self theWordSize:str andFont:font andCGSize:CGSizeMake(Kwidth - 30, 1000)];
    if (allContent == nil) {
        allContent = [[UILabel alloc] init];
        allContent.numberOfLines = 0;
        allContent.text = str;
        allContent.font = [UIFont systemFontOfSize:13];
        [contentScroll addSubview:allContent];
    } else {
        allContent.text = str;
    }
    allContent.frame = CGRectMake(15, 55, Kwidth - 30, size.height);
    
    if (deadColorView == nil) {
        deadColorView = [[UIView alloc] init];
        deadColorView.backgroundColor = HexRGB(0xe1e2e2);
    }
    deadColorView.frame = CGRectMake(0, allContent.frame.origin.y + allContent.frame.size.height + 18, Kwidth, 1);
    [contentScroll addSubview:deadColorView];
    
    // 文字“怀孕推荐”
    if (tuijian == nil) {
        tuijian = [[UILabel alloc] init];
        tuijian.text = @"饮食推荐";
        tuijian.textColor = HexRGB(0xed4f78);
        tuijian.font = [UIFont boldSystemFontOfSize:17];
        [contentScroll addSubview:tuijian];
    }
    tuijian.frame  = CGRectMake(15, deadColorView.frame.origin.y + 20, 100, 18);

    // 显示详细信息
    str = [[contentDic objectForKey:[NSString stringWithFormat:@"%d", whichWeekInt + 1]] lastObject];
    str = [str stringByReplacingOccurrencesOfString:@"\\r" withString:@"\r"];
    str = [str stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    size = [self theWordSize:str andFont:font andCGSize:CGSizeMake(Kwidth - 30, 1000)];
    if (StarContent == nil) {
        StarContent = [[UILabel alloc] init];
        StarContent.numberOfLines = 0;
        StarContent.text = str;
        StarContent.font = [UIFont systemFontOfSize:13];
        [contentScroll addSubview:StarContent];
    } else {
        StarContent.text = str;
    }
    StarContent.frame = CGRectMake(15, tuijian.frame.origin.y + tuijian.frame.size.height + 12, Kwidth - 30, size.height);
    
    contentScroll.contentSize = CGSizeMake(Kwidth, StarContent.frame.origin.y + StarContent.frame.size.height + 20);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self changeCurrentViewData];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self changeCurrentViewData];
}

- (void)changeCurrentViewData {
    CGPoint point = whichWeek.contentOffset;
    int whichWeekPoint = (point.x - 32) / 64 + 1;
    whichWeek.contentOffset = CGPointMake(whichWeekPoint * 64, 0);
    [self showContentScroll:whichWeekPoint];
}

//返回上一个页面
- (void)popToPreviousView {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
